package net.lahuobao.mall.controller.admin;

import net.lahuobao.export.util.ExportFileUtil;
import net.lahuobao.mall.entity.admin.MallShopSettlementDetailsResultEntity;
import net.lahuobao.mall.entity.admin.MallShopSettlementResultEntity;
import net.lahuobao.mall.entity.admin.querymodel.MallShopSettlementQueryEntity;
import net.lahuobao.mall.service.admin.MallShopSettlementServiceI;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.util.ContextHolderUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.cgform.exception.BusinessException;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by clark on 2016/9/18 0018.
 */
@Controller("mallShopSettlementController")
@RequestMapping("/mallShopSettlementController")
public class MallShopSettlementController extends BaseController {

    @Autowired
    private MallShopSettlementServiceI settlementService;

    @Resource
    private ExportFileUtil exportFileUtil;

    /**结算列表
     * DataGrid dataGrid
     * @return
     */
    @RequestMapping(params = "list")
    public ModelAndView list(){
        return new ModelAndView("net/lahuobao/mall/admin/mallShopSettlementList");
    }

    /**
     * 列表dataGrid
     * @param query
     * @param response
     * @param dataGrid
     */
    @RequestMapping(params = "datagrid")
    public void datagrid(MallShopSettlementQueryEntity query,HttpServletRequest request, HttpServletResponse response,DataGrid dataGrid){

        //输出dataggrid
        TagUtil.datagrid(response, pageDateGrid(selectListByquery(request, query), dataGrid));
    }

    /**
     * 导出结算
     * @param query
     * @param response
     */
    @RequestMapping(params = "export")
    public void export(MallShopSettlementQueryEntity query,HttpServletRequest request, HttpServletResponse response){

        exportFileUtil.setFileName("商户结算", "xlsx");
        // 构造数据导出
        try {
            int page = 1;
            int rows = 1000;
            exportFileUtil.initExport(response);
            request.setAttribute("pageExport", "yes");
            request.setAttribute("page", page);
            request.setAttribute("rows", rows);
            int begin_ = exportFileUtil.exportIOStream(response, selectListByquery(request, query) , rows, page, 0);
            for(int i = 2; begin_ != page; i++ ){
                page = begin_;
                request.setAttribute("page", i);
                begin_ = exportFileUtil.exportIOStream(response, selectListByquery(request, query), rows, begin_, 1);
            }
            exportFileUtil.finish(response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    /**
     * 查询数据
     * @param query
     * @param request
     * @return
     */
    private List<MallShopSettlementResultEntity> selectListByquery(HttpServletRequest request, MallShopSettlementQueryEntity query){
        //设置结算时间
        if(StringUtil.isNotEmpty(request.getParameter("settlementTime_begin"))){
            query.setSettlementStartTime(request.getParameter("settlementTime_begin"));
        }

        if(StringUtil.isNotEmpty(request.getParameter("settlementTime_end"))){
            query.setSettlementEndTime(request.getParameter("settlementTime_end"));
        }
        //设置交易时间
        if(StringUtil.isNotEmpty(request.getParameter("createTime_begin"))){
            query.setCreateStartTime(request.getParameter("createTime_begin"));
        }

        if(StringUtil.isNotEmpty(request.getParameter("createTime_end"))){
            query.setCreateEndTime(request.getParameter("createTime_end"));
        }

        return  settlementService.selectManageShopSettlement(query);
    }


    /**结算列表
     * DataGrid dataGrid
     * @return
     */
    @RequestMapping(params = "detailslist")
    public ModelAndView details(HttpServletRequest request, String  shopId){
        request.setAttribute("shopId", shopId);
        return new ModelAndView("net/lahuobao/mall/admin/mallShopSettlementDetailsList");
    }


    /**
     * 店铺结算清单
     * @param query
     */
    @RequestMapping(params = "detailsdatagrid")
    public void details(HttpServletRequest request, HttpServletResponse response, MallShopSettlementQueryEntity query, DataGrid dataGrid){

        TagUtil.datagrid(response, pageDateGrid(selectDetailsList(request, query), dataGrid));

       /* //统计
        SettlementQueryModel totalQueryModel = new SettlementQueryModel();
        totalQueryModel.setShopId(manageShopSettlementQueryModel.getShopId());
        totalQueryModel.setSettlementStatus(manageShopSettlementQueryModel.getSettlementStatus());
        totalQueryModel.setAuditStatus(manageShopSettlementQueryModel.getAuditStatus());
        ShopSettlementResultModel resultModel = mallShopSettlementService.selectShopSettlementStatistics(totalQueryModel);*/

       /* mv.addObject("shopSettlementDetails", shopSettlementList);
        mv.addObject("total", resultModel);
        mv.addObject("queryModel", manageShopSettlementQueryModel);*/
    }

    /**
     * 查询商家结算清单
     * @param query
     * @param request
     */
    private List<MallShopSettlementDetailsResultEntity> selectDetailsList(HttpServletRequest request, MallShopSettlementQueryEntity query){
        return  settlementService.selectManageShopSettlementDetails(query);
    }


    /**
     * 确认结算
     * @Param shopId
     */
    @RequestMapping(params = "confirmSettlement", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson confirmSettlement(String shopId, Integer id){
        AjaxJson json = new AjaxJson();
        try {
            HttpSession session = ContextHolderUtils.getSession();
            TSUser user = (TSUser)session.getAttribute(ResourceUtil.LOCAL_CLINET_USER);
            settlementService.confirmSettlement(shopId, id, -1);//Integer.valueOf(user.getId());
            json.setMsg("提前申请处理成功");
        } catch (BusinessException e) {
            json.setSuccess(false);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }catch (Exception e){
            json.setSuccess(false);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }finally {
            return json;
        }
    }


    /**
     * 导出商户结算清单
     * @param query
     * @param response
     */
    @RequestMapping(params = "detailsexport")
    public void detailsexport(MallShopSettlementQueryEntity query,HttpServletRequest request, HttpServletResponse response){

         exportFileUtil.setFileName("商户结算清单", "xlsx");
        // 构造数据导出
        try {
             int page = 1;
            int rows = 1000;
            exportFileUtil.initExport(response);
            request.setAttribute("pageExport", "yes");
            request.setAttribute("page", page);
            request.setAttribute("rows", rows);
            int begin_ = exportFileUtil.exportIOStream(response, selectDetailsList(request, query) , rows, page, 0);
            for(int i = 2; begin_ != page; i++ ){
                page = begin_;
                request.setAttribute("page", i);
                begin_ = exportFileUtil.exportIOStream(response, selectDetailsList(request, query), rows, begin_, 1);
            }
            exportFileUtil.finish(response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }



}
